package com.hhh.sun.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hhh.sun.pojo.entity.TbCharging;
import com.hhh.sun.service.ChargingService;
import com.hhh.sun.vo.resp.PageResult;
import com.hhh.sun.vo.resp.R;
import com.hhh.sun.vo.resp.ResponseCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 充电站操作接口
 */
@Api(value = "/sun", tags = {"充电站操作接口"})
@RestController
@RequestMapping("/sun")
public class ChargingController {
    @Autowired
    private ChargingService chargingService;

    /**
     * 分页查询所有的充电站信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "pageNum", value = "当前页数"),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "pageSize", value = "每页大小")
    })
    @ApiOperation(value = "分页查询所有的充电站信息", notes = "分页查询所有的充电站信息", httpMethod = "GET")
    @GetMapping("/chargings")
    public R<PageResult<TbCharging,TbCharging>> getAllChargingInfo(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize){
        return chargingService.findAll(pageNum,pageSize);
    }
    /**
     * 添加充电站
     */

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "name", value = "", required = true)
    })
    @ApiOperation(value = "添加充电站", notes = "添加充电站", httpMethod = "POST")
    @PostMapping("/charging/{name}")
    public R<Object>addCharging(@PathVariable("name")String name){
        TbCharging c = chargingService.getOne(Wrappers.<TbCharging>lambdaQuery().eq(TbCharging::getChargingName, name));
        if(c!=null){
            return R.error(ResponseCode.CHARGING_EXISTS_ERROR);
        }
        c=new TbCharging();
        c.setChargingName(name);
        boolean b = chargingService.save(c);
        return b?R.ok(ResponseCode.SUCCESS):R.error(ResponseCode.ERROR);
    }
    /**
     * 修改充电站是否可用
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "name", value = "", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "op", value = "", required = true)
    })
    @ApiOperation(value = "修改充电站是否可用", notes = "修改充电站是否可用", httpMethod = "PUT")
    @PutMapping("/charging/{name}/{op}")
    public R<Object>update(@PathVariable("name")String name,@PathVariable("op")Integer op){
        boolean b = chargingService.update(Wrappers.<TbCharging>lambdaUpdate().eq(TbCharging::getChargingName, name).set(TbCharging::getIsenabled, op));
        return b?R.ok(ResponseCode.SUCCESS):R.error(ResponseCode.ERROR);
    }
    /**
     * 删除重充电站
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "name", value = "", required = true)
    })
    @ApiOperation(value = "删除充电站", notes = "删除充电站", httpMethod = "DELETE")
    @DeleteMapping("/charging/{name}")
    public R<Object>deleteByName(@PathVariable("name")String name){
        boolean b = chargingService.remove(Wrappers.<TbCharging>lambdaQuery().eq(TbCharging::getChargingName, name));
        return b?R.ok(ResponseCode.SUCCESS):R.error(ResponseCode.ERROR);
    }
}
